package com.rainman.simpleredis.bootstart.configuration;

import com.rainman.simpleredis.core.SimpleRedis;
import com.rainman.simpleredis.core.scheduled.SimpleScheduled;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.nio.charset.Charset;


/**
 * 监听redis过期key
 *
 */
@Lazy(value = false)
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    SimpleRedis simpleRedis;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }


    @Override
    public void onMessage(Message message, byte[] pattern) {


        String key = new String(message.getBody(), Charset.forName( simpleRedis.getSimpleConfig().getCharset()));

        simpleRedis.dealTimeOutTig(key);


        super.onMessage(message, pattern);
    }


}
